var EXTENSION_ID = "extension-01";
var DEFAULT_GLOBAL_FREQUENCY = "300000";
var DEFAULT_FREQUENCY = "86400000";
var SECURITY_LENGTH = 5;
var REMOTE_URL = 'http://achatell.free.fr/';
var STATIC_STORAGE = ['timestamp', 'displayed', 'definitionList', 'display-date', 'frequency'];
var DYNAMIC_STORAGE = [{name: 'definitionList', types: ['frequency', 'definition', 'displayed']}];
var STORAGE_SEPARATOR = '|';

function createCoreDefinition(name, definition){	
	name = getNameWithUppercase(name);
	var isNameExist = storageGet('definitionList').split('|').some(
		function (element, index, array) {
			return (element == name);
		}
	);
	if(name == ""){
		return "Please enter a name.";
	} else if(isNameExist){
		return "This name already exists, please choose an other one.";
	} else {
		if(storageGet('definitionList') != ""){
			storageSet('definitionList', storageGet('definitionList') + '|' + name);	
		} else {
			storageSet('definitionList', storageGet('definitionList') + name);	
		}
		$('#create-definition-word').val("");
		
		if(definition){
			storageSet(name + '-definition', definition);
		} else {
			storageSet(name + '-definition', "");
		}
		storageSet(name + '-frequency', DEFAULT_FREQUENCY);
		return "";
	}
}

function remove(element) {
	removeDynamicElement('definitionList', element);
	init();
}

function reset(element, callback) {
	storageSet(element + '-frequency', DEFAULT_FREQUENCY);
	if(callback){
		callback();
	}
}

function resetAll(){
	if(storageGet('definitionList') != ""){
		$.each(
			storageGet('definitionList').split('|'),
			function(intIndex, element){
				reset(element);
			}
		);
	}
	init();
}

function createDefinition() {
	var createName = $('#create-definition-word').val();
	var message = createCoreDefinition(createName);
	if(message != ""){
		alert(message);
	}
	init();
}